package com.example.adopt.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.adopt.entity.Adoptinfo;
import com.example.adopt.entity.Adoptor;
import com.example.adopt.entity.Pet;
import com.example.adopt.mapper.PetMapper;
import com.example.adopt.service.AdoptinfoService;
import com.example.adopt.service.AdoptorService;
import com.example.adopt.service.PetService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wjl
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/adoptor")
public class AdoptorController {
    @Autowired
    AdoptorService adoptorService;
    @Resource
    ObjectMapper objectMapper;
    @Resource
    PetService petService;
    @Resource
    AdoptinfoService adoptinfoService;
    @RequestMapping("/getAdoptor")
    public String getAdoptor(String petname,String adoptorname,
                                Integer page,Integer size) throws JsonProcessingException {
        QueryWrapper<Adoptor> wrapper = new QueryWrapper<>();
        if(!StrUtil.isBlank(petname)){
            wrapper.like("petname", petname);
        }
        if(!StrUtil.isBlank(adoptorname)){
            wrapper .like("adoptorname", adoptorname);
        }
        Page<Adoptor> aPage = new Page<>(page, size);
        IPage<Adoptor> aIPage = adoptorService.page(aPage,wrapper);
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(aIPage);
        return s;
    }
    @RequestMapping("/getAdoptorS_1")
    public String getAdoptorS_1(String petname,String adoptorname,
                             Integer page,Integer size) throws JsonProcessingException {
        QueryWrapper<Adoptor> wrapper = new QueryWrapper<>();
        wrapper.inSql("petname", "SELECT petname FROM pet WHERE isadopt = 1");
        if(!StrUtil.isBlank(petname)){
            wrapper.like("petname", petname);
        }
        if(!StrUtil.isBlank(adoptorname)){
            wrapper .like("adoptorname", adoptorname);
        }
        Page<Adoptor> aPage = new Page<>(page, size);
        IPage<Adoptor> aIPage = adoptorService.page(aPage,wrapper);
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(aIPage);
        return s;
    }
    @RequestMapping("/getAdoptorS_3")
    public String getAdoptorS_3(String petname,String adoptorname,
                                Integer page,Integer size) throws JsonProcessingException {
        QueryWrapper<Adoptor> wrapper = new QueryWrapper<>();
        wrapper.inSql("petname", "SELECT petname FROM pet WHERE isadopt = 3");
        if(!StrUtil.isBlank(petname)){
            wrapper.like("petname", petname);
        }
        if(!StrUtil.isBlank(adoptorname)){
            wrapper .like("adoptorname", adoptorname);
        }
        Page<Adoptor> aPage = new Page<>(page, size);
        IPage<Adoptor> aIPage = adoptorService.page(aPage,wrapper);
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(aIPage);
        return s;
    }
    @RequestMapping("/getAdoptorById")
    public String getAdoptorById(String adoptorid) throws JsonProcessingException {
        QueryWrapper<Adoptor> adoptorQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Adoptor> adoptorQueryWrapper1 = adoptorQueryWrapper.eq("adoptorid", adoptorid);
        Adoptor adoptor = adoptorService.getOne(adoptorQueryWrapper1);
        String s = objectMapper.writeValueAsString(adoptor);
        return s;
    }
    @DeleteMapping("/delAdoptor")
    public String delAdoptor(String adoptorid){
        boolean remove = adoptorService.removeById(adoptorid);
        String result = remove == true ? "success" : "false";
        return result;
    }
    @PutMapping("/updateAdpotor")
    public String updateAdpotor(String adoptorid,String adoptorname,
                                String adoptorphone,String adoptorage,String petname){
        QueryWrapper<Adoptor> adoptorQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Adoptor> adoptorQueryWrapper1 = adoptorQueryWrapper.eq("adoptorid", adoptorid);
        Adoptor adoptor = adoptorService.getOne(adoptorQueryWrapper1);
        adoptor.setAdoptorage(adoptorage);
        adoptor.setAdoptorname(adoptorname);
        adoptor.setPetname(petname);
        adoptor.setAdoptorphone(adoptorphone);
        boolean remove = adoptorService.updateById(adoptor);
        String result = remove == true ? "success" : "false";
        return result;
    }
    @PutMapping("/adoptOk")
    public String adoptOk(String adoptorname,String petname){
        String s = UUID.randomUUID().toString().replaceAll("-", "");
        String s1 = new Date().toLocaleString();
        QueryWrapper<Pet> petQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Pet> petname1 = petQueryWrapper.eq("petname", petname);
        Pet pet= petService.getOne(petname1);
        pet.setIsadopt(2);
        boolean b = petService.updateById(pet);
        if(b==true){
            Adoptinfo adoptinfo = new Adoptinfo();
            adoptinfo.setAdoptinfoid(s);
            adoptinfo.setAdoptorname(adoptorname);
            adoptinfo.setPetname(petname);
            adoptinfo.setStatus(2);
            adoptinfo.setTransfertime(" ");
            adoptinfo.setAdopttime(s1);
            boolean save = adoptinfoService.save(adoptinfo);
            if(save==true){
                return "审核成功";
            }else {
                return "审核失败2";
            }
        }else {
            return "审核失败1";
        }
    }
    @PutMapping("/transferOk")
    public String transferOk(String adoptorname,String petname){
        String s = UUID.randomUUID().toString().replaceAll("-", "");
        String s1 = new Date().toLocaleString();
        QueryWrapper<Pet> petQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Pet> petname1 = petQueryWrapper.eq("petname", petname);
        Pet pet= petService.getOne(petname1);
        pet.setIsadopt(4);
        boolean b = petService.updateById(pet);
        if(b==true){
            QueryWrapper<Adoptinfo> adoptinfoQueryWrapper = new QueryWrapper<>();
            QueryWrapper<Adoptinfo> eq = adoptinfoQueryWrapper.eq("adoptorname", adoptorname).eq("petname", petname);
            Adoptinfo adoptinfo = adoptinfoService.getOne(eq);
            adoptinfo.setStatus(4);
            adoptinfo.setTransfertime(s1);
            boolean save = adoptinfoService.updateById(adoptinfo);
            if(save==true){
                return "审核成功";
            }else {
                return "审核失败2";
            }
        }else {
            return "审核失败1";
        }
    }

}

